package com.atguigu.day21.charstream03;

import org.junit.Test;

import java.io.*;
import java.util.Arrays;

/*
IO: input output 输入和输出
分类:
    流向分:
         输入和输出
    数据分:
        字节和字符
    角色分:
        节点和处理
四大基流 (抽象类):
   字节流
       InputStream
       OutputStream
   字符流:
       Reader
       Writer
字节:
    字节输出流:
        以字节为单位写出数据
           字节输出流对象.write(数字)
           字节输出流对象.write(字节数组)
    字节输入流:以字节为单位读取数据
         read() 一次读取一个字节 没有数据返回-1
         read(byte[]) 一次读取一个字节数组 没有数据返回-1
字符:
    Reader:字符输入流
          read() 一次读取一个字符  没有数据 返回 -1
          read(char[]) 一次读取一个字符数组 没有数据 返回-1
    Writer:字符输出流



 */
public class ReaderTest {

    @Test
    public void test05(){
        //1.准备流对象
        try (Reader reader = new FileReader("D:/b.txt")) {
            //2.准备数组
                char[] cArr = new char[5];
            //3.循环读 并展示
            int length = 0;
            while ((length=reader.read(cArr))!=-1){

                String s = new String(cArr, 0, length);
                System.out.println("s = " + s);
            }


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }


    @Test
    public void test04(){
        //1.准备流对象
        try (Reader reader = new FileReader("D:/b.txt")) {
            //2.读取数据并展示
            char[]cArr = new char[5];
            System.out.println("读取前 = " + Arrays.toString(cArr));
            int length = reader.read(cArr);
            System.out.println("1 读取后 = " + Arrays.toString(cArr)+",length = "+length);
            length = reader.read(cArr);
            System.out.println("2 读取后 = " + Arrays.toString(cArr)+",length = "+length);
            length = reader.read(cArr);
            System.out.println("3 读取后 = " + Arrays.toString(cArr)+",length = "+length);
            length = reader.read(cArr);
            System.out.println("4 length = " + length);


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }




    }



    @Test
    public void test03(){
        //1.准备流对象
        try (Reader reader = new FileReader("D:/b.txt")) {
            //2.循环读 并展示
            int value = 0;
            while ((value=reader.read())!=-1){
                System.out.println("value = " + value+","+(char)value);

            }



        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }




    }


    @Test
    public void test02(){
        try (Reader reader = new FileReader("D:/a.txt")) {

            int value = reader.read();
            System.out.println("1 value = " + value+","+(char)value);
            value = reader.read();
            System.out.println("2 value = " + value+","+(char)value);
            value = reader.read();
            System.out.println("3 value = " + value+","+(char)value);
            value = reader.read();
            System.out.println("4 value = " + value);

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test01(){
        try (InputStream is = new FileInputStream("D:/a.txt")) {

            int value = is.read();
            System.out.println(value+" , "+(char)value);


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
